//
//  MahjongGameGangView.m
//  Mahjong
//
//  Created by 饶首建 on 17/7/25.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "MahjongGameGangView.h"
#import "HWPopTool.h"
#import "ControlButton.h"

#import <Masonry/Masonry.h>

typedef enum : NSUInteger {
    GangType_angang,
    GangType_zhigang,
    GangType_bugang,
} GangType;

@interface MahjongGameGangView()<UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet UIView *gangBtnContainer;


@end

@implementation MahjongGameGangView

- (void)awakeFromNib{
    [super awakeFromNib];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [[HWPopTool sharedInstance]closeWithBlcok:^{}];
    }];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
}

+(instancetype)mahjongGameGangView{
    return [[[NSBundle mainBundle] loadNibNamed:@"MahjongGameGangView"
                                          owner:nil options:nil]lastObject];
}

- (void)configData{
    if (_isZhigang) {
        ControlButton *cbz = [[ControlButton alloc]initWithTitle1:@"" fontSize:60 cornerRadius:0];
        [cbz setImage:UIImageWithName(@"gang_zhigang") forState:UIControlStateNormal];
        [_gangBtnContainer addSubview:cbz];
        cbz.backgroundColor = [UIColor whiteColor];
        cbz.layer.cornerRadius = 6.f;
        [cbz mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(222);
            make.height.offset(130);
            make.top.offset(20);
            make.centerX.equalTo(_gangBtnContainer.mas_centerX);
            make.centerY.equalTo(_gangBtnContainer.mas_centerY);
        }];
        
        [cbz addTapBlock:^(UIButton *btn) {
            [self submitAction:GangType_zhigang];
        }];
    }else{
        ControlButton *cba = [[ControlButton alloc]initWithTitle1:@"" fontSize:60 cornerRadius:0];
        [cba setImage:UIImageWithName(@"gang_angang") forState:UIControlStateNormal];
        cba.backgroundColor = [UIColor whiteColor];
        cba.layer.cornerRadius = 6.f;
        ControlButton *cbb = [[ControlButton alloc]initWithTitle1:@"" fontSize:60 cornerRadius:0];
        [cbb setImage:UIImageWithName(@"gang_bugang") forState:UIControlStateNormal];
        cbb.backgroundColor = [UIColor whiteColor];
        cbb.layer.cornerRadius = 6.f;
        [_gangBtnContainer addSubview:cba];
        [_gangBtnContainer addSubview:cbb];
        [cba mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(222);
            make.height.offset(150);
            make.right.equalTo(_gangBtnContainer.mas_centerX).offset(-20);
            make.top.offset(20);
        }];
        [cbb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(222);
            make.height.offset(150);
            make.left.equalTo(_gangBtnContainer.mas_centerX).offset(20);
            make.top.offset(20);
        }];
        
        [cba addTapBlock:^(UIButton *btn) {
            [self submitAction:GangType_angang];
        }];
        [cbb addTapBlock:^(UIButton *btn) {
            [self submitAction:GangType_bugang];
        }];
    }
}
- (IBAction)closeAction:(id)sender {
    [[HWPopTool sharedInstance] closeWithBlcok:^{}];
}

- (void)submitAction:(GangType)type {
    NSString *url = NSStringFormat(@"%@/Games/%@/actions",BASE_URL,_gameModel.gameId);
    NSMutableArray *tmp = [NSMutableArray new];
    for (PlayerModel *pm in _selectedPlayerArr) {
        [tmp addObject:@{
                         @"id":pm.playerId,
                         @"name":pm.playerName
                         }];
    }
    
    NSString *attr = @"直杠";
    switch (type) {
        case GangType_angang:
            attr = @"暗杠";
            break;
        case GangType_bugang:
            attr = @"补杠";
            break;
        default:
            break;
    }
    
    NSDictionary *param = @{
                            @"type":@"杠",
                            @"attributes":@[attr],
                            @"owner":@{
                                    @"id":_owner.playerId,
                                    @"name":_owner.playerName
                                    },
                            @"targets":tmp
                            };
    [HttpUtil sendRequestURL:url method:POST param:param success:^(id response) {
        [[HWPopTool sharedInstance] closeWithBlcok:^{}];
    } fail:^(NSString *errorStr) {
        [Hud hudText:errorStr inView:self];
    }];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    // 取消响应手势
    if (touch.view == self){
        return YES;
    }
    return NO;
}

@end
